<?php

namespace app\common\controller;

use think\Controller;
use think\Db;
use think\cache\driver\Redis;

class Crontab extends Controller
{
    protected $time;
    protected $redis;

    public function _initialize()
    {
        parent::_initialize();
        set_time_limit(0);
        $this->time = time();
        $this->redis = new Redis();
    }


    /**
     * 获取开奖数据并执行后续操作
     */
    public function index()
    {
        $data_json = file_get_contents("http://207.148.41.138/getkjjg.php");
        $this->redis->set('kjjg',$data_json);

        $data_all = json_decode($data_json, true);
        $data_redis = array();
        if (!$data_all['rows']) return '无数据';
        /**写入redis*/
        foreach ($data_all['data'] as $v) {
            $resd = array();
            switch ($v['code']) {
                case 'bjkl8':
                    $resd = $this->kjjgDataDeal('10', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'ahk3':
                    $resd = $this->kjjgDataDeal('36', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'jsk3':
                    $resd = $this->kjjgDataDeal('44', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'jx11x5':
                    $resd = $this->kjjgDataDeal('25', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'sd11x5':
                    $resd = $this->kjjgDataDeal('28', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'gd11x5':
                    $resd = $this->kjjgDataDeal('16', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'cqssc':
                    $resd = $this->kjjgDataDeal('1', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'tjssc':
                    $resd = $this->kjjgDataDeal('8', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'bjpk10':
                    $resd = $this->kjjgDataDeal('4', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'mlaft':
                    $resd = $this->kjjgDataDeal('61', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'pl3':
                    $resd = $this->kjjgDataDeal('5', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'qxc':
                    $resd = $this->kjjgDataDeal('51', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'shssl':
                    $resd = $this->kjjgDataDeal('48', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'fc3d':
                    $resd = $this->kjjgDataDeal('7', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
	            case 'hk6':
                    $resd = $this->kjjgDataDeal('9', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;	

					//新加彩种
				case 'xjssc'://新疆时时彩 2
                    $resd = $this->kjjgDataDeal('2', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'sh11x5'://上海11选5
                    $resd = $this->kjjgDataDeal('29', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'gsk3'://甘肃快3 
                    $resd = $this->kjjgDataDeal('39', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
                case 'gxk3'://广西快3
                    $resd = $this->kjjgDataDeal('40', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'gzk3'://贵州快3 
                    $resd = $this->kjjgDataDeal('41', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'hubk3'://湖北快3 43
                    $resd = $this->kjjgDataDeal('43', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'jlk3'://吉林快3 46
                    $resd = $this->kjjgDataDeal('46', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;
				case 'shk3'://上海快3
                    $resd = $this->kjjgDataDeal('47', $v['opencode'], $v['expect'], $v['opentimestamp']);
                    break;

            }
            if($resd){
                if( !in_array($v['code'],$data_redis) ){
                    $this->redis->set("result_{$resd['czid']}", "{$resd['qihao']},{$resd['jztime']},{$resd['kjjg']}");
                    $data_redis[] = $v['code'];
                }
                $this->kjjgSave($resd);
            }
        }


        // ------------start------------ //
        //此处可定义新接口处理私彩
        // ------------end------------- //

        echo "<br/>---------- 处理当期数据完成 ----------<br/>";
      //  $this->processingNew();
      //        $this->processingNewXyft();

    }

    /**
     * 处理开奖数据
     */
    private function kjjgDataDeal($czid, $kjjgns, $qihao, $opentimestamp)
    {
        $kjjg_arr = explode(',', $kjjgns);
        $kjjg_string = implode('', $kjjg_arr);

        if ($czid == '10') {
            $kl8_arr = $kjjg_arr;
            //北京快乐8转换成pc蛋蛋
            $sum_1 = $kl8_arr[0] + $kl8_arr[1] + $kl8_arr[2] + $kl8_arr[3] + $kl8_arr[4] + $kl8_arr[5];
            $num_1 = substr($sum_1, strlen($sum_1) - 1, 1);
            $sum_2 = $kl8_arr[6] + $kl8_arr[7] + $kl8_arr[8] + $kl8_arr[9] + $kl8_arr[10] + $kl8_arr[11];
            $num_2 = substr($sum_2, strlen($sum_2) - 1, 1);
            $sum_3 = $kl8_arr[12] + $kl8_arr[13] + $kl8_arr[14] + $kl8_arr[15] + $kl8_arr[16] + $kl8_arr[17];
            $num_3 = substr($sum_3, strlen($sum_3) - 1, 1);
            $num_4 = $num_1 + $num_2 + $num_3;
            $kjjg_string = $num_1 . $num_2 . $num_3 . '=' . $num_4;
        }

        if ($czid == '9') {
            $kjjg_string = str_replace('+', '', $kjjg_string);
        }

        /* 
        //北京快乐8的开奖结果处理
        if($czid == '60') {
            $kjjg_arr[19] = substr($kjjg_arr[19],0,2);
            unset($kjjg_arr[19]);
            $kjjg_string = join('',$kjjg_arr);
        }
        */
        return array('czid' => $czid, 'qihao' => $qihao, 'jztime' => $opentimestamp, 'kjjg' => $kjjg_string);
    }

    /**
     * 执行开奖数据入库操作
     */
    private function kjjgSave($data)
    {
        $czid = $data['czid'];
        $qihao = $data['qihao'];
        $jztime = $data['jztime'];
        $kjjg_string = $data['kjjg'];

        // 查询是否存在本期
        $exist = Db::name('kjjg_list')->where(['qihao' => $qihao, 'czid' => $czid])->find();

        if ($exist) {
            //0投注中 1停投 2已退
            if (!$exist['iftz']) {
                Db::name('kjjg_list')->where('id', $exist['id'])->data(['kjjg' => $kjjg_string, 'iftz' => 1])->update();
                echo "{$czid} 号彩第 {$qihao} 期数据更新成功<br/>";
            }
        } else {
            Db::name('kjjg_list')->data(['czid' => $czid, 'qihao' => $qihao, 'jztime' => $jztime, 'kjjg' => $kjjg_string, 'ps' =>'开奖写入','iftz' => 1])->insert();
            echo "{$czid} 号彩第 {$qihao} 期数据写入成功<br/>";
        }
        usleep(10000);
    }




}
